<?php
namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Response;
use app\common\model\UserModel;
use app\admin\model\User as userInfo;

class User extends Common
{
    public function user()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }

            $where['is_real'] = ['eq',1];
            if (isset($pagedata['user_tel']) && $pagedata['user_tel'] != '') {
                $where['phone'] = array('like', '%' . $pagedata['user_tel'] . '%');
            }
             if (isset($pagedata['nickname']) && $pagedata['nickname'] != '') {
                $where['nickname'] = array('like', '%' . $pagedata['nickname'] . '%');
            }  
            if (isset($pagedata['create_time']) && $pagedata['create_time'] != ''){
                $created_at=explode(' - ', $pagedata['create_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                }
              $where['create_time']=['between time',$created_at]; 
            }  

            $user = new UserModel();
            $data = $user->getUserInfo($where, $startrow, $pagesize);
            $count = $user->getUserCount($where);
            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
            return view();
        }

    }

    public function editpass()
    {
        if (Request::instance()->isAjax()) {
            $data = Request::instance()->post();
            $data = $data['data'];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $hashPassword = $hasher->HashPassword($data['password']);
            $where = "uid = {$data['uid']}";
            $user = new UserInfo();
            $res = $user->editPass($where, $hashPassword);
            if ($res) {
                return 1;
            } else {
                return 2;
            }
        }
    }

    public function delUser()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "uid= {$post['uid']}";
            $user = new UserInfo();
            $res = $user->del($where);
            if ($res) {
                return "1";
            } else {
                return "2";
            }
        }
    }


    public function userInfoDetail()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $where = "uid= {$post['uid']}";
            $field = 'rename,cardid,create_time';
            $user = new UserInfo();
            $userInfo = $user->lookUserInfo($where, $field);
            if ($userInfo) {
                return json(array('code' => 0, 'data' => $userInfo));
            } else {
                return json(array('code' => 1));
            }
        }
    }
    /*
     * 是否启用
     */
    public function isStart()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $data = [
                'is_del'=>$post['is_del'],
                'token'=>'1',
            ];
            $res = Db::name('user')->where('uid',$post['uid'])->update($data);
            if($res){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }

    /*
     * 假用户数据
     */
    public function false_user()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }

            $where['is_real'] = ['eq',0];
            if (isset($pagedata['user_tel']) && $pagedata['user_tel'] != '') {
                $where['phone'] = array('like', '%' . $pagedata['user_tel'] . '%');
            }

            $data =  Db::name('user')->where($where)
                ->limit($startrow, $pagesize)
               ->select();
            $count = DB::name('user')->where($where)->count();
            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
            return view();
        }

    }

}